$(function($) {
	loadedSize = 1;
	maxSize = 6;
	page = 1;
	gif_click= function(){
		$('#trollsContent').find('.gif-post').each(function() {
			$(this).click(function() {
				var parent = $(this).parent();
				$(parent).find('.hide').removeClass('hide');
				$(this).addClass("hide");
			});

		});
	};
	
	load_photo = function(){
		$('#trollsContent').scrollPagination({
			'contentPage' : "/loadTroll",
			'scrollTarget' : $(window),
			'heightOffset' : 400,
			'beforeLoad' : function() {
				var url = document.URL;
				if(url.lastIndexOf('/new/') != -1){
					page = parseInt(url.substring(url.lastIndexOf('/')+1));
					if(isNaN(page)){
						page = 0;
					}
					console.log(page);
					
				}
				$('#loader').fadeIn();
				if (loadedSize != $('#trollsContent').children().size()) {
					loadedSize = $('#trollsContent').children().size();
					$(this)[0].contentData = {
						loadedSize : loadedSize + (page-1)*maxSize
					};
				} else {
					$(this)[0].contentData = {
						loadedSize : -1
					};
				}
			},
			'afterLoad' : function(elementsLoaded) {
				$('#loader').fadeOut();
				if(typeof FB != 'undefined'){
					FB.XFBML.parse();
				}
				if($('#trollsContent').children().size() >= maxSize){
					$('#trollsContent').stopScrollPagination();
					if($('#load-more-btn')[0] == undefined){
						$('#mainBox').append("<div id='load-more-btn' onclick='loadNew(" + (page + 1)+ ")' class='btn btn-primary buttons submitButton'>Tải Thêm Hình</div>");
					}
				}
				gif_click();
			}
		});
	};
	
	loadNew = function(page){
		window.location.href = "/new/"+ page;
	};
});
